﻿namespace WaferService
{
    public class LoadingPositionShim(IMotionControlService motionControlService,
       IPLCService plcService,
       ILogger<LoadingPositionShim> logger)
    {
        int mod;

        public void Start()
        {
            var v = Interlocked.Increment(ref mod);
            Task.Run(async () =>
            {
                try
                {
                    while (Volatile.Read(ref mod) == v)
                    {
                        //判断是否在Loading位置
                        if (await motionControlService.IsInLoadingPos())
                        {
                            await plcService.SetPLCStatus(WaferStatus.Set_StageInLoadingPos, "true");
                        }
                        else
                        {
                            await plcService.SetPLCStatus(WaferStatus.Set_StageInLoadingPos, "false");
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "Error setting Set_StageInLoadingPos");
                    return;
                }
            });
        }

        public void Stop()
        {
            Interlocked.Increment(ref mod);
        }
    }
}
